namespace MessageBroker.tests

import System
import MessageBroker
import MessageBroker.Client
import MessageBroker.Server
import System.Collections
import NUnit.Framework

[TestFixture]
class MessageBrokerTests:
"""Description of MessageBrokerTests"""

	firstSubscription = "la mia prima coda"
	emptySubscription = "una coda vuota"
	massiveSubscription = "coda piena di roba"
	referralName = "ciao sono io..."
	firstSharedQueue = "firstSharedQueue"
	broker = Broker(
				SharedQueues: 
					{
					"firstSharedQueue":
						SharedQueue(
							Name: "firstSharedQueue"
							)
					}
			    )
	client = BrokerClient(
				Server:broker, 
				InstanceName: 
				referralName, 
				MessagesPerPage: 50, 
				UseBufferedSending: true
				)

	client2 = BrokerClient(
				Server:broker, 
				InstanceName: 
				referralName + "#2", 
				MessagesPerPage: 200, 
				UseBufferedSending: true
				)

	[TestFixtureSetUp]
	def setup():
		broker.CreateSubscription(firstSubscription, referralName)
		broker.CreateSubscription(massiveSubscription, referralName)
		broker.CreateSubscription(massiveSubscription, referralName + "#2")
		broker.CreateSubscription(emptySubscription, referralName)
		client.Send(firstSubscription,Message(Subject:"test", Body: "test body"))
		
	[TestFixtureTearDown]
	def tearDown():
		print "sono stati mandati ${client.SentMessagesCount} messaggi"
		
	def sendMessage(subject as string):
		client.Send(massiveSubscription, Message(Subject:"subject ${subject}", Body: "test body"))
		
	def receiveMessage(subject as string):
		message = client.Receive(massiveSubscription)
		assert message != null	

	def doManyMessages(many as int):
		for i in range(many):
			sendMessage("test numero ${i}")
		client.Flush()
		for i in range(many):
			receiveMessage("test numero ${i}")			

	[Test]
	def do1000Messages():
		doManyMessages(1000)

	[Test]
	def do10000Messages():
		doManyMessages(10000)

	[Test]
	def do100000Messages():
		doManyMessages(100000)
		
	[Test]
	def NoMessagesToReceive():
		message = client.Receive(emptySubscription)
		assert message == null

	[Test]
	def Receive():
		message as Message
		while (message == null):
			message = client.Receive(firstSubscription)
		assert message != null
		assert message.Subject.Equals("test")
		assert message.Body.Equals("test body")
		
		howMany = 100
		for i in range(howMany):
			sendMessage("test numero ${i}")
			
		client.Flush()
			
		count = 0 
		while count < howMany:
			questoGiro = client.BufferedReceive(massiveSubscription).Count
			assert questoGiro <= client.MessagesPerPage
			count += questoGiro
			print questoGiro
			break if questoGiro == 0
			
		print count
		assert count == howMany
		
	[Test]
	def ReplyToMessage():
		msg = Message(Subject: "ciao")
		client.Send(firstSubscription, msg)
		client.Flush()
		rcvdMsg = client.Receive(firstSubscription)
		answer = rcvdMsg.CreateReply()
		client.Send(firstSubscription, answer)
		client.Flush()
		reply = client.Receive(firstSubscription)
		assert reply.InReplyTo == rcvdMsg

	[Test]
	def ReceiveFromClient2():
		quanti = 0
		while true:
			ret = client2.Receive(100)
			if (ret != null and ret.Count > 0): 
				quanti += ret.Count
				for msg as Message in ret:
					assert msg.Subject != null
			else:
				print "ricevuti da client2 ${quanti} messaggi"
				return
				
	[Test]
	def UseSharedQueue():
		sentMsg = Message(Subject: "ciao")
		client.Send(firstSharedQueue, sentMsg)
		client.Flush()
		rcvdMsg = client2.Receive(firstSharedQueue)
		assert rcvdMsg.Id.Equals(sentMsg.Id)
		rcvdMsg = client2.Receive(firstSharedQueue)
		assert rcvdMsg == null
